// ============================================================================
// 通知系统Hook
// ============================================================================

import { useCallback } from 'react'

/**
 * 通知数据接口
 */
interface NotificationData {
  id?: string
  type: 'info' | 'success' | 'warning' | 'error'
  title: string
  message: string
  duration?: number
  actions?: Array<{
    label: string
    action: () => void
  }>
}

/**
 * 通知系统Hook
 */
export function useNotifications() {
  /**
   * 显示通知
   */
  const showNotification = useCallback((notification: NotificationData) => {
    // 生成唯一ID
    notification.duration = notification.duration || 1000 * 3;
    const id = notification.id || `notification-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
    
    // 创建通知事件
    const notificationEvent = new CustomEvent('show-notification', {
      detail: {
        ...notification,
        id
      }
    })
    
    // 分发事件
    window.dispatchEvent(notificationEvent)
    
    return id
  }, [])

  /**
   * 隐藏通知
   */
  const hideNotification = useCallback((id: string) => {
    const hideEvent = new CustomEvent('hide-notification', {
      detail: { id }
    })
    
    window.dispatchEvent(hideEvent)
  }, [])

  /**
   * 清除所有通知
   */
  const clearAllNotifications = useCallback(() => {
    const clearEvent = new CustomEvent('clear-all-notifications')
    window.dispatchEvent(clearEvent)
  }, [])

  /**
   * 快捷方法
   */
  const showSuccess = useCallback((title: string, message: string, duration?: number) => {
    const notification: NotificationData = { type: 'success', title, message }
    if (duration !== undefined) {
      notification.duration = duration
    }
    return showNotification(notification)
  }, [showNotification])

  const showError = useCallback((title: string, message: string, duration?: number) => {
    const notification: NotificationData = { type: 'error', title, message }
    if (duration !== undefined) {
      notification.duration = duration
    }
    return showNotification(notification)
  }, [showNotification])

  const showWarning = useCallback((title: string, message: string, duration?: number) => {
    const notification: NotificationData = { type: 'warning', title, message }
    if (duration !== undefined) {
      notification.duration = duration
    }
    return showNotification(notification)
  }, [showNotification])

  const showInfo = useCallback((title: string, message: string, duration?: number) => {
    const notification: NotificationData = { type: 'info', title, message }
    if (duration !== undefined) {
      notification.duration = duration
    }
    return showNotification(notification)
  }, [showNotification])

  return {
    showNotification,
    hideNotification,
    clearAllNotifications,
    showSuccess,
    showError,
    showWarning,
    showInfo
  }
}
